package com.example.forum2.Service.impl;

import com.example.forum2.Service.IBoardService;
import com.example.forum2.comment.AppResult;
import com.example.forum2.comment.ResultCode;
import com.example.forum2.dao.BoardMapper;
import com.example.forum2.exception.ApplicationExeception;
import com.example.forum2.model.Board;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class IBoardServiceImpl implements IBoardService {
    @Autowired
    private BoardMapper boardMapper;
    @Override
    public void addOneArticleCountById(Long id) {
      if(id==null||id==0 ){
         throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
      }
        Board board = boardMapper.selectByPrimaryKey(id);
      if(board==null){
          throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
      }
      Board board1 = new Board();
      board1.setId(board.getId());
      board1.setArticleCount(board.getArticleCount()+1);
      int row = boardMapper.updateByPrimaryKeySelective(board1);
      if(row!=1){
          throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
      }
    }

    @Override
    public Board selectById(Long id) {
        if(id==null||id<=0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
        return boardMapper.selectByPrimaryKey(id);
    }

    @Override
    public void subOneArticleCountByID(Long id) {
        if(id==null||id<=0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        Board board1 = new Board();
        board1.setId(board.getId());
        board1.setArticleCount(board.getArticleCount()-1);
        if(board1.getArticleCount()<0){
            board1.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(board1);
        if(row!=1){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void create(Board board) {
        if (board == null) {
            throw new ApplicationExeception(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        // 设备初值
        board.setArticleCount(0); // 帖⼦数量
        board.setState((byte) 0); // 状态
        board.setDeleteState((byte) 0);

        Date date = new Date();
        board.setCreateTime(date); // 创建时间
        board.setUpdateTime(date); // 更新时间
        int row = boardMapper.insertSelective(board); if (row != 1) {
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_USER_BANNED));

        }

    }


    @Override
    public List<Board> selectByNum(Integer num) {
        if(num<=0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
        }
        return boardMapper.selectByNum(num);
    }

    @Override
    public List<Board> selectAllNormal() {
        return boardMapper.selectAllNormal();
    }
}
